jQuery(function(){
	jQuery("#editor").after("<div id='taskpane'><div class='tab'><span>Task</span></div><div class='inner'/></div>");
	//alert("asdads");
	
	$("#taskpane .tab").click(function(){
		if($("#taskpane .inner").height()){
			$("#taskpane .inner").animate({height:0});
			$("#send-answer").css({color:"transparent"});
		}else{
			$("#taskpane .inner").animate({height:250});
			$("#send-answer").css({color:"#0088CC"});
		}
	});
	
	$("#create-task-btn").click(function(){
		var type=$("#task-type").val();
		if(type=="0"){
			return false;	
		}
		var task=createTaskFromForm(type);
		if(!task)
			return;
		var id=uniqueId();
		Skt.model.addRoom('question',{roomId:id});
		Skt.tmanager.addTask(id,task);
		$('#add-task-modal').modal('hide');
		$('span.tmp').remove();
		$(".tr-type span .radio").prop({'checked':false});
		$(".tr-type span input[type='text']").val('');
		$("#task-description").val('');
		$("#taskpane").show();	
	});
	
	$("#task-type").change(function(){
		var type=$("#task-type").val();
		$(".tr-type").hide();
		$("#tr-type-"+type).show();
	});
	
	$("#add-option-2").click(function(){
		var span=$("<span class='tmp'/>");
		var a=$("<a href='#'><i class='icon-minus'></i></a>");
		a.click(function(){
			span.remove();
		});
		span.append(a).append('<input type="radio" class="radio" name="so-option"/><input type="text" placeholder="Alternative Text"/>');;
		$("#tr-type-2").append(span);
		
		
	});
	
	$("#add-option-3").click(function(){
		var span=$("<span class='tmp'/>");
		var a=$("<a href='#'><i class='icon-minus'></i></a>");
		a.click(function(){
			span.remove();
		});
		span.append(a).append('<input type="checkbox" class="radio"/><input type="text" placeholder="Alternative Text"/>');;
		$("#tr-type-3").append(span);
		
		
	});

	$("#taskpane").hide();
	
});

function createTaskFromForm(type){
	var context=$("#tr-type-"+type);
	var valid=false;
	var options=[];
	var number=1;
	$("span",context).each(function(){
		
		var checked=$(".radio",$(this)).is(":checked");
		if(checked)
			valid=true;
		var text=$("input[type='text']",$(this)).val();
		if($.trim(text).length==0){
			valid=false;
			return false;
		}
		options.push({value:text,answer:checked,number:number});
		number=number+1;
		
	});
	if(!valid)
		return null;
	return {options:options,type:type,instructions:$("#task-description").val()};

}
function getAnswer(){
	var answer=[];
	$("#taskpane .inner input:checked").each(function(){
		answer.push(parseInt($(this).val()));
	});
	return answer;
}

Skt.tmanager = new L.TasksManager({
	enabled : Skt.coupling.enabled,
	adapter : Skt.coupling.activityAdapter,
	carriedId : "taskManager"
});



var old_setCurrentCanvas=Skt.sketchpad.setCurrentCanvas;
Skt.sketchpad.setCurrentCanvas=function(id, following){
	old_setCurrentCanvas.apply(Skt.sketchpad,[id, following]);
	console.log('Skt.tmanager.getRoomTask(id)');
	console.log(Skt.tmanager.getRoomTask(id));
	if ( Skt.tmanager.getRoomTask(id) ) {
		$("#taskpane").show();
		Skt.sketchpad.showTaskData({roomId:id});
		$("#taskpane .inner").animate({height:0});
		$("#send-answer").css({color:"transparent"});
	} else {
		$("#taskpane").hide();
		$("#taskpane .inner").html('');
	}
	//alert("it works");
};

Skt.sketchpad.showTaskData = function(attr_list){
	if ( !Skt.config.USE_UI )
		return;
	var roomId = attr_list.roomId;
	var task=Skt.tmanager.getRoomTask(roomId);
	$("#taskpane .inner").html('');
	if(task.instructions){
		$("#taskpane .inner").append($("<div class='i1'><p>"+task.instructions+"</p></div>"));
	}
	if(task.type!=1){
		var div=$("<div/>").css({"width":'100%'});
		$.each(task.options,function(id,option){
			var op=$("<div/>").addClass("option");
			var cb=$("<input type='checkbox' value='"+option.number+"'/>");
			
			if(task.type==2){
				cb=$("<input type='radio' name='option' value='"+option.number+"'/>");
			}
			if(Skt.user.type=="teacher"){
				cb.prop({"disabled":true,'checked':option.answer});
			}
			if(task.answered(Skt.user.id)){
				console.log(task.answered(Skt.user.id));
				//alert($.inArray(option.number,task.answered(Skt.user.id)));
				cb.prop({"disabled":true,'checked':$.inArray(option.number,task.answered(Skt.user.id).answer)>=0});
			}
			var td=$("<td/>").append(cb);
			var tr=$("<tr><td><p>"+option.value+"</p></td></tr>");
			tr.prepend(td);
			var table=$("<table/>").append(tr);
			op.append(table).appendTo(div);
		});
		$("#taskpane .inner").append(div);
	}
	$("#taskpane").show();
	$("#taskpane #send-answer").remove();
	var sa=$("<a id='send-answer'/>");
	$("#taskpane .tab").append(sa);
	if(Skt.user.type=="teacher"){
		if(task.parent==null && task.instances.length==0){
			sa.html("Publish").append("<i class='icon-check'></i>").click(function(){
				Skt.tmanager.publish(task.id);
			});
		}
		if(task.parent!=null || task.instances.length>0){
			sa.html("Results").append("<i class='icon-check'></i>");
		}
	}else{
		if(!task.answered(Skt.user.id)){
			sa.html("Answer").append("<i class='icon-check'></i>").click(function(){
				Skt.tmanager.answer(task.id,Skt.user.id, Skt.user.name+" "+Skt.user.lastname,getAnswer());
				Skt.sketchpad.showTaskData(attr_list);
			});
		}
		
	}
};
